Hyunjung Im
Frontend Developer
2022-12-23
var twoSum = function (nums, target) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] === target) {
return [left, right];
}
if (left === right - 1) {
left++;
right = nums.length - 1;
} else {
right--;
}
}
};
var twoSum2 = function (nums, target) {
const storedNumberAndIndex = {};
nums.forEach((number, index) => (storedNumberAndIndex[number] = index));
for (let i = 0; i < nums.length; i++) {
if (storedNumberAndIndex[target - nums[i]] && storedNumberAndIndex[target - nums[i]] !== i) {
return [storedNumberAndIndex[target - nums[i]], i];
}
}
};